require(["common", "jQuery", "coAjax", "queryString", "href", "hash_routie"], function(jSouper, $, coAjax, QueryString, href, hash_routie) {
	var _num = 10;
	var _page = 0;
	var _card_factory_id;

	function _load_goods_page(card_factory_id, num, page) {
		if (card_factory_id) {
			var url = appConfig.card_factory.goods_list_with_page_info + card_factory_id;
		} else {
			url = appConfig.goods.list_with_page_info;
		}
		coAjax.get(url, {
			bus_id: busInfo._id,
			num: num,
			page: page,
			show_un_sold_goods: true
		}, function(result) {
			var info = result.result;
			console.log("bus.goods", info.goods);
			App.set("goods", info.goods);
			var page_info = {
				total_num: info.total_num,
				total_page: info.total_page,
				page: info.page,
				num: info.num
			};
			page_info._number_list = [];
			page_info._number_list.length = info.total_page;
			App.set("$Cache.bus.goods.page_info", page_info);
		});
	};

	function _init_goods_data() {
		//获取会员卡，每张卡对应一个专区
		coAjax.get(appConfig.bus.card_factory_list, function(result) {
			App.set("$Cache.bus.goodsmanager.card_factory_list", result.result);
		});
		var queryString = new QueryString(location.hash);
		_num = queryString.get("num") || _num;
		_page = ~~queryString.get("page");
		_card_factory_id = queryString.get("card_factory_id");
		_load_goods_page(_card_factory_id, _num, _page);
	};
	//分页功能
	hash_routie.on("goodsmanager", _init_goods_data);

	//删除商品
	App.set("$Event.remove_goods", function(e, vm) {
		myConfirm("确定删除“" + vm.get("goods_name") + "”", function(result) {
			var url = _card_factory_id ? appConfig.bus.card_factory_remove_goods + _card_factory_id : appConfig.bus.remove_goods;
			coAjax["delete"](url, {
				goods_id: vm.get("_id"),
				num: _num,
				page: _page,
				show_un_sold_goods: true
			}, function(result) {
				var goods = result.result;
				if (!(goods instanceof Array)) {
					goods = goods.goods;
				}
				App.set("goods", goods);
			});
		}, function() {
			console.log("取消删除");
		});
	});
	//修改商品
	App.set("$Event.update_goods", function(e, vm) {
		App.set("$Cache.goods_to_update", vm.get());
		location.hash = "admin/update_goods?id=" + vm.get("_id");
	});

	function _update_goods_index(goods_id, goods_index) {
		if (_card_factory_id) {
			var url = appConfig.bus.card_factory_update_goods_index + _card_factory_id;
		} else {
			url = appConfig.bus.update_goods_index;
		}
		coAjax.put(url, {
			goods_id: goods_id,
			goods_index: goods_index
		}, function() {
			_init_goods_data();
		});
	};
	//置顶商品
	App.set("$Event.to_top", function(e, vm) {
		_update_goods_index(vm.get("_id"), 0);
	});
	App.set("$Event.to_up", function(e, vm) {
		_update_goods_index(vm.get("_id"), (~~vm.get("$Index") + _num * _page) - 1);
	});
	App.set("$Event.to_down", function(e, vm) {
		_update_goods_index(vm.get("_id"), (~~vm.get("$Index") + _num * _page) + 1)
	});
	//拖动排序
	App.set("$Event.goodsmanager.dragstart", function(e, vm) {
		console.log("dragstart");
		vm.set("_is_draging", true);
		App.set("$Cache.goodsmanager._is_dragenter", true)
		App.set("$Cache.goodsmanager.dragging_index", vm.get("$Index"));
	});
	App.set("$Event.goodsmanager.dragenter", function(e, vm) {});
	App.set("$Event.goodsmanager.dragleave", function(e, vm) {});
	App.set("$Event.goodsmanager.dragend", function(e, vm) {
		vm.set("_is_draging", false);
		App.set("$Cache.goodsmanager._is_dragenter", false);
		var dragend_index = App.get("$Cache.goodsmanager.dragend_index") + _num * _page;

		_update_goods_index(vm.get("_id"), dragend_index);
		console.log("dragend", dragend_index);
	});
	App.set("$Event.goodsmanager.dragover", function(e, vm) {
		// console.log(App.get("$Cache.goodsmanager._is_dragenter"));
		if (!App.get("$Cache.goodsmanager._is_dragenter")) {
			return;
		}
		var self = $(this);
		var offset = self.offset();
		var height = self.height();
		var y = e.pageY;
		// console.log(offset, e);
		var index = vm.get("$Index");
		var arr = vm.get("$Parent");
		var dragging_index = App.get("$Cache.goodsmanager.dragging_index");
		// console.log(y, offset.top, height,y <= (offset.top + (height / 2)));
		if (y <= (offset.top + (height / 2))) { //鼠标在上半部分
			var pre_status = "dragenter-status-before";
			var cur_status = "dragenter-status-after";
			var aft_status = "";
			var dragend_index = (dragging_index < index) ? (index - 1) : index
		} else { //在下半部分
			var pre_status = "";
			var cur_status = "dragenter-status-before";
			var aft_status = "dragenter-status-after"
			var dragend_index = (dragging_index > index) ? (index + 1) : index
		}
		console.log(dragend_index, dragging_index, index);
		App.set("$Cache.goodsmanager.dragend_index", dragend_index);
		if (arr[index]._dragenter_status == cur_status) { //无需改变状态
			return
		}
		jSouper.forEach(arr, function(item) {
			item._dragenter_status = null;
		});
		arr[index]._dragenter_status = cur_status;
		if (index > 0) {
			arr[index - 1]._dragenter_status = pre_status;
		}
		if (index < arr.length - 1) {
			arr[index + 1]._dragenter_status = aft_status;
		}
		vm.set("$Parent", arr);
	});
	//上架
	App.set("$Event.goodsmanager.remove_goods_from_un_sold_goods", function(e, vm) {
		coAjax.put(appConfig.bus.remove_goods_from_un_sold_goods, {
			goods_id: vm.get("_id")
		}, function(result) {
			vm.set(result.result.goods)
		});
	});
	//下架
	App.set("$Event.goodsmanager.add_goods_to_un_sold_goods", function(e, vm) {
		coAjax.put(appConfig.bus.add_goods_to_un_sold_goods, {
			goods_id: vm.get("_id")
		}, function(result) {
			vm.set(result.result.goods)
		});
	});
});